<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>water_electricity</title>
    <link rel="stylesheet" href="../../../static/layer/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../../static/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../../static/ucss/hydropower/water_electricity.css">
    <style>
        .layui-anim-upbit {
            max-height: 300px;
            overflow-y: auto;
        }
    </style>
</head>

<body>
<div class="layui-tab layui-tab-brief">
    <ul class="layui-tab-title">
        <li class="layui-eletric layui-this">电表设置</li>
        <li class="layui-water">水表设置</li>
    </ul>
    <div class="layui-tab layui-tab-card">
        <ul class="layui-nav layui-tab-title layui-bg-gray" id="busibuild">
        </ul>
    </div>
    <div class="layui-tab-content">
    </div>
</div>
<script type="text/javascript" src="../../../static/layer/layui.js"></script>
<script>
    var table;
    var $;
    var element;
    var form;
    var activebuildingID = 'f2b6a2dab0214a51b418b1f4de9435a8';//选中楼宇ID
    var priceId;//水电表价格Id
    var TYPE = 'b938139a48ed4344a11a7180b48eede6';
    layui.use(['jquery', 'table', 'form', 'layer', 'element', 'tree'], function () {
        table = layui.table;
        $ = layui.jquery;
        element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
        form = layui.form;
        $('.layui-eletric').click(function () {
           initEleTable();
        })
        $('.layui-water').click(function () {
            initWatTable();
        })

        /*******商圈楼宇********/
        var active = {
            changeBusiness: function (obj) { //获取选中数据
                activebuildingID = obj.data("id");
                if(TYPE == 'b938139a48ed4344a11a7180b48eede6'){
                    initEleTable();
                }else {
                    initWatTable();
                }

//                getEleTree();
//                getWaterTree();
//                getHydropowerPrice();
            }
        }
        $.ajax({
            url: "/business/list",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0") {
                    $(".layui-nav").empty();
                    var off = '<span class="layui-badge-rim">未开启</span>';
                    var str = '';
                    for (var i = 0; i < res.data.length; i++) {
                       /* if (res.data[i].IS_OPEN === "on") {
                            str = '<li class="layui-nav-item" data-id="' + res.data[i].ID + '"><a href="javascript:;">' + res.data[i].BUSINESS_NAME + '</a><dl class="layui-nav-child"></dl></li>';
                        } else if (res.data[i].IS_OPEN === "off") {
                            str = '<li class="layui-nav-item" data-id="' + res.data[i].ID + '"><a href="javascript:;">' + res.data[i].BUSINESS_NAME + off + '</a><dl class="layui-nav-child"></dl></li>';
                        }*/
                        str = '<li class="layui-nav-item" data-id="' + res.data[i].ID + '"><a href="javascript:;">' + res.data[i].BUSINESS_NAME + '</a><dl class="layui-nav-child"></dl></li>';
                        $(".layui-nav").append(str);
                    }
                } else if (res.errcode === "1") {
                    layer.msg(res.errmsg, function () {
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });

        $.ajax({
            url: "/building/list",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0") {
                    var off = '<span class="layui-badge-rim">未开启</span>';
                    for (var i = 0; i < $(".layui-nav-item").length; i++) {
                        for (var j = 0; j < res.data.length; j++) {
                            if (res.data[j].BUSINESS_ID === $($(".layui-nav-item")[i]).attr("data-id")) {
                                var child = "";
                               /* if (res.data[j].IS_OPEN === "on") {
                                    child = '<dd data-type="changeBusiness" data-id="' + res.data[j].BUILDING_ID + '"><a href="javascript:;">' + res.data[j].BUILDING_NAME + '</a></dd>';
                                } else if (res.data[j].IS_OPEN === "off") {
                                    child = '<dd data-type="changeBusiness" data-id="' + res.data[j].BUILDING_ID + '"><a href="javascript:;">' + res.data[j].BUILDING_NAME + off + '</a></dd>';
                                }*/
                                child = '<dd data-type="changeBusiness" data-id="' + res.data[j].BUILDING_ID + '"><a href="javascript:;">' + res.data[j].BUILDING_NAME + '</a></dd>';
                                $(".layui-nav-item").eq(i).find(".layui-nav-child").append(child);
                            }
                        }
                    }
                    $('.layui-nav .layui-nav-item dl dd').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type]($(this)) : '';
                    });
                    element.init();
                    $(".layui-nav .layui-nav-item dl dd").eq(0).click();
                } else if (res.errcode === "1") {
                    layer.msg(res.errmsg, function () {
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });

        $("#elesave").click(function () {
            var eletricPrice = $("#eleprice").val();
            updatePrice(eletricPrice, null);
        })
        $("#watersave").click(function () {
            var waterPrice = $("#waterprice").val();
            updatePrice(null, waterPrice);
        })

        //tree背景色修改
        $("body").on("mousedown",".layui-tree a cite",function(){
            $(".layui-tree a cite").css('color','black')
            $(this).css('color','red');
        })
    });

    /**
     * 打开新增或修改页面
     * @param t 0-电表；1-水表
     * @param s 0-新增水（电）表；1-新增子表
     * @param y 0-新增；1-修改
     */
    function add(t, s, y) {
        var title;
        var id = '';
        var pid = '';
        var pCode = '';
        if (y == 0) {
            if (s == 0) {
                if (t == 0) {
                    title = "新增电表";
                } else {
                    title = "新增水表";
                }
            } else {
                title = "新增子表";
                if (t == 0) {
                    pid = $("#eletable").find("tbody").find("tr").eq(0).attr("id");
                    pCode = $("#eletable").find("tbody").find("tr").eq(0).find("td").eq(0).text();
                } else {
                    pid = $("#watertable").find("tbody").find("tr").eq(0).attr("id");
                    pCode = $("#watertable").find("tbody").find("tr").eq(0).find("td").eq(0).text();
                }
            }
        } else {
            var msg = "";
            if (t == 0) {
                title = "修改电表";
                msg = "请先选择要操作的电表";
                id = $("#eleinfo").attr("code");
            } else {
                title = "修改水表";
                msg = "请先选择要操作的水表";
                id = $("#watinfo").attr("code");
            }
            if(id == null || id == ""){
                layer.msg(msg);
                return false;
            }
        }
        layer.open({
            type: 2,
            title: title,
            shadeClose: false,
            shade: [0.3],
            maxmin: true, //开启最大化最小化按钮
            area: ['900px', '90%'],
            scrollbar: false, //屏蔽滚动条
            content: 'water_electricity_edit.html?t=' + t + '&s=' + s + '&y=' + y + '&id=' + id + '&pid=' + pid + '&pCode=' + pCode + '&buildingId=' + activebuildingID
        });
    }

    function getTreeNodes(type, buildingId) {
        var nodes;
        $.ajax({
            type: "post",
            url: "/hydropower/findbypid",
            async: false,
            data: {
                buildingId: buildingId,
                type: type
            },
            success: function (rs) {
                nodes = rs.data;
                if(nodes != null && nodes.length>0){
                    if(type == "b938139a48ed4344a11a7180b48eede6"){
                        getPowerInfo(0,nodes[0].id);
                    }else {
                        getPowerInfo(1,nodes[0].id);
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
        return nodes;
    }

    function getPowerInfo(type, id) {
        $.ajax({
            type: "post",
            url: "/hydropower/findbyidfor",
            async: true,
            data: {
                id: id
            },
            success: function (rs) {
                var data = rs.data;
                var id = "";
                if (type == 0) {
                    id = "#eleinfo";
                }else {
                    id = "#watinfo";
                }
                $(id).attr("code",data.ID);
                $(id).find(".info-item").eq(0).find(".item-data").find("span").text(data.CODE);
                $(id).find(".info-item").eq(1).find(".item-data").find("span").text(data.BUILDING_NAME);
                $(id).find(".info-item").eq(2).find(".item-data").find("span").text(data.RATES);
                $(id).find(".info-item").eq(3).find(".item-data").find("span").text(data.CREATE_DATE);
                $(id).find(".info-item").eq(4).find(".item-data").find("span").text(isCard(data.IS_CARD));
                $(id).find(".info-item").eq(5).find(".item-data").find("span").text(clearUndefined(data.REMARKS));
                /*if (data != null) {
                    var tr = $("<tr id='" + data.ID + "'></tr>");
                    var td1 = $("<td>" + data.CODE + "</td>");
                    var td2 = $("<td>" + data.BUILDING_NAME + "</td>");
                    var td3 = $("<td>" + data.RATES + "</td>");
                    var td4 = $("<td>" + data.CREATE_DATE + "</td>");
                    var td5 = $("<td>" + clearUndefined(data.REMARKS) + "</td>");
                    var td6 = $("<td></td>");
//                    var bt1 = $("<button class='layui-btn' onclick='add(" + type + ",1,0)'>新增子表</button>");
                    var bt2 = $("<button class='layui-btn layui-btn-warm' onclick='add(" + type + ",0,1)'>修改</button>");
                    var bt3 = $("<button class='layui-btn layui-btn-danger' onclick='del(" + type + ")'>删除</button>");
                    var bt4 = $("<button class='layui-btn layui-btn-primary' onclick='changeMeter(" + type + ")'>换表</button>");
//                    td6.append(bt1);
                    td6.append(bt2);
                    td6.append(bt4);
                    td6.append(bt3);
                    tr.append(td1);
                    tr.append(td2);
                    if (type == 0) {
                        tr.append(td3);
                    }
                    tr.append(td4);
                    tr.append(td5);
                    tr.append(td6);
                    if (type == 0) {
                        $("#eletable").find("tbody").empty();
                        $("#eletable").find("tbody").append(tr);
                    } else {
                        $("#watertable").find("tbody").empty();
                        $("#watertable").find("tbody").append(tr);
                    }
                } else {
                    if (type == 0) {
                        $("#eletable").find("tbody").append($("<td colspan='4'>暂无数据</td>"));
                    } else {
                        $("#watertable").find("tbody").append($("<td colspan='4'>暂无数据</td>"));
                    }
                }*/
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
    }


    /*function del(t) {
        var id;
        var tips;
        var msg = "";
        if (t == 0) {//电表
            id = $("#eleinfo").attr("code");
            tips = "确定要删除该电表吗";
            msg = "请先选择要操作的电表";
        } else {
            tips = "确定要删除该水表吗";
            id = $("#watinfo").attr("code");
            msg = "请先选择要操作的水表";
        }
        if(id == null || id == ""){
            layer.msg(msg);
            return false;
        }
        layer.msg(tips, {
            time: 0
            , btn: ['确定', '取消']
            , shade: 0.3
            , yes: function (index) {
                layer.close(index);
                $.ajax({
                    type: "post",
                    url: "/hydropower/del",
                    async: false,
                    data: {
                        id: id
                    },
                    success: function (rs) {
                        if (rs.errcode == "00") {
                            if (t == 0) {
                                getEleTree();
                                id = "#eleinfo";
                            } else {
                                getWaterTree();
                                id = "#watinfo";
                            }
                            $(id).attr("code",'');
                            $(id).find(".info-item").eq(0).find(".item-data").find("span").text('');
                            $(id).find(".info-item").eq(1).find(".item-data").find("span").text('');
                            $(id).find(".info-item").eq(2).find(".item-data").find("span").text('');
                            $(id).find(".info-item").eq(3).find(".item-data").find("span").text('');
                            $(id).find(".info-item").eq(4).find(".item-data").find("span").text('');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                    }
                });
            }
        });

    }*/

    function getEleTree() {
        $("#electricity").empty();
        layui.tree({
            elem: '#electricity' //传入元素选择器
            , nodes: getTreeNodes('b938139a48ed4344a11a7180b48eede6', activebuildingID)
            , click: function (node) {
                getPowerInfo(0, node.id);
            }
        });
    }
    function getWaterTree() {
        $("#water").empty();
        layui.tree({
            elem: '#water' //传入元素选择器
            , nodes: getTreeNodes('ae5806f8184546208411b99e6117eaa2', activebuildingID)
            , click: function (node) {
                getPowerInfo(1, node.id);
            }
        });
    }

    function clearUndefined(val) {
        if (typeof(val) == "undefined") {
            return " ";
        }
        return val;
    }

    function getHydropowerPrice() {
        $.ajax({
            type: "post",
            url: "/hydropowerprice/findhydropowerprice",
            async: false,
            data: {
                buildingId: activebuildingID
            },
            success: function (rs) {
                var data = rs.data;
                if (data != null) {
                    priceId = data.ID;
                    $("#eleprice").val(data.ELETRIC_PRICE);
                    $("#waterprice").val(data.WATER_PRICE);
                    if (data.ELETRIC_PRICE != null) {
                        $("#elesave").val("修改")
                    }
                    if (data.WATER_PRICE != null) {
                        $("#watersave").val("修改")
                    }
                } else {
                    $("#eleprice").val('');
                    $("#waterprice").val('');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
    }

    function updatePrice(eletricPrice, waterPrice) {
        $.ajax({
            type: "post",
            url: "/hydropowerprice/save",
            async: false,
            data: {
                id: priceId,
                buildingId: activebuildingID,
                eletricPrice: eletricPrice,
                waterPrice: waterPrice
            },
            success: function (rs) {
                if (rs.errcode == "01") {
                    layer.msg(rs.errmsg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
    }

    function getCodeBusiness() {
        var business = $("#busibuild").find(".layui-this").find("a").html();
        var codefix = business.substring(0, business.indexOf("<"));
        return codefix;
    }
    function getCodeBuild() {
        var build = $("#busibuild").find(".layui-this").find("dl").find(".layui-this").find("a").html();
        return build;
    }
    function changeMeter(t) {
        var id = "";
        var title = "";
        var msg = "";
        if (t == 0) {
            title = "更换电表";
            msg = "请先选择要操作的电表";
            id = $("#eleinfo").attr("code");
        } else {
            title = "更换水表";
            msg = "请先选择要操作的水表";
            id = $("#watinfo").attr("code");
        }
        if(id == null || id == ""){
            layer.msg(msg);
            return false;
        }
        layer.open({
            type: 2,
            title: title,
            shadeClose: false,
            shade: [0.3],
            maxmin: true, //开启最大化最小化按钮
            area: ['900px', '90%'],
            scrollbar: false, //屏蔽滚动条
            content: 'water_electricity_change.html?t=' + t + '&id=' + id
        });
    }
    //是否插卡式
    function isCard(val) {
        var info = "";
        if(val == 1){
            info = "是";
        }else {
            info = "否";
        }
        return info;
    }

    function initEleTable() {
        $('.layui-tab-content').empty();
        TYPE = "b938139a48ed4344a11a7180b48eede6";
        $('.layui-tab-content').load('/hydropower/waterTabel?type='+TYPE+'&buildingId='+activebuildingID);
    }
    function initWatTable() {
        TYPE = "ae5806f8184546208411b99e6117eaa2";
        $('.layui-tab-content').empty();
        $('.layui-tab-content').load('/hydropower/waterTabel?type='+TYPE+'&buildingId='+activebuildingID);
    }
</script>
</body>

</html>